<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Python笔记 | lilei</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/images/icons/logo.png">
    <link rel="apple-touch-icon" href="/images/icons/logo.png">
    <link rel="manifest" href="/manifest.json">
    <script type="text/javascript" src="/assets/js/load.js"></script>
    <meta name="description" content="个人网站">
    <meta name="twitter:title" content="Python笔记">
    <meta name="twitter:description" content="">
    <meta name="twitter:card" content="summary">
    <meta name="twitter:url" content="http://www.lileiblog.xyz/Python/Python%E7%AC%94%E8%AE%B0.html">
    <meta property="og:type" content="article">
    <meta property="og:title" content="Python笔记">
    <meta property="og:description" content="">
    <meta property="og:url" content="http://www.lileiblog.xyz/Python/Python%E7%AC%94%E8%AE%B0.html">
    <meta property="og:site_name" content="lilei">
    <meta itemprop="name" content="Python笔记">
    <meta itemprop="description" content="">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileImage" content="/images/icons/logo.png">
    <meta name="msapplication-TileColor" content="#ffffff">
    <meta name="keywords" content="lilei,vue,vuepress,vuepress-theme,theme,主题,vuepress主题,blog
        ,vuepress-blog,java,python,shell,sql,golang,script,shell script,nginx,windows,去广告,androd,google drive
        ,one drive,idea,eclipse,git,小程序">
    <meta name="referrer" content="never">
    
    <link rel="preload" href="/assets/css/0.styles.8932179b.css" as="style"><link rel="preload" href="/assets/js/app.269009d6.js" as="script"><link rel="preload" href="/assets/js/3.974b9720.js" as="script"><link rel="preload" href="/assets/js/45.79178b33.js" as="script"><link rel="prefetch" href="/assets/js/10.5915e99b.js"><link rel="prefetch" href="/assets/js/11.ff732762.js"><link rel="prefetch" href="/assets/js/12.8bb86d91.js"><link rel="prefetch" href="/assets/js/13.18bde5b0.js"><link rel="prefetch" href="/assets/js/14.314fae34.js"><link rel="prefetch" href="/assets/js/15.0a4ff48f.js"><link rel="prefetch" href="/assets/js/16.ffaf1503.js"><link rel="prefetch" href="/assets/js/17.49a31a6e.js"><link rel="prefetch" href="/assets/js/18.7e6443bf.js"><link rel="prefetch" href="/assets/js/19.6880cc9a.js"><link rel="prefetch" href="/assets/js/20.1fb3f255.js"><link rel="prefetch" href="/assets/js/21.0cd39eea.js"><link rel="prefetch" href="/assets/js/22.392d85cb.js"><link rel="prefetch" href="/assets/js/23.f50adb15.js"><link rel="prefetch" href="/assets/js/24.43e324e1.js"><link rel="prefetch" href="/assets/js/25.e3663205.js"><link rel="prefetch" href="/assets/js/26.da141fb8.js"><link rel="prefetch" href="/assets/js/27.6c05235a.js"><link rel="prefetch" href="/assets/js/28.c124245a.js"><link rel="prefetch" href="/assets/js/29.d185870d.js"><link rel="prefetch" href="/assets/js/30.8587d422.js"><link rel="prefetch" href="/assets/js/31.dc382b56.js"><link rel="prefetch" href="/assets/js/32.20ba32e9.js"><link rel="prefetch" href="/assets/js/33.6ecd7de9.js"><link rel="prefetch" href="/assets/js/34.2b91c13a.js"><link rel="prefetch" href="/assets/js/35.66ed0733.js"><link rel="prefetch" href="/assets/js/36.ce7bca84.js"><link rel="prefetch" href="/assets/js/37.d00bb5ac.js"><link rel="prefetch" href="/assets/js/38.c3ecbb6f.js"><link rel="prefetch" href="/assets/js/39.308f4175.js"><link rel="prefetch" href="/assets/js/4.520f6ecc.js"><link rel="prefetch" href="/assets/js/40.f4b98bc2.js"><link rel="prefetch" href="/assets/js/41.95fae64f.js"><link rel="prefetch" href="/assets/js/42.fe4f877b.js"><link rel="prefetch" href="/assets/js/43.ca8f222e.js"><link rel="prefetch" href="/assets/js/44.4d4ee79e.js"><link rel="prefetch" href="/assets/js/46.c3817d0b.js"><link rel="prefetch" href="/assets/js/47.b8252443.js"><link rel="prefetch" href="/assets/js/48.df9d5d3b.js"><link rel="prefetch" href="/assets/js/49.d812fdb2.js"><link rel="prefetch" href="/assets/js/5.f1a24a16.js"><link rel="prefetch" href="/assets/js/50.55cfcaa8.js"><link rel="prefetch" href="/assets/js/51.b8608b02.js"><link rel="prefetch" href="/assets/js/52.52c5e00d.js"><link rel="prefetch" href="/assets/js/53.3a9bd5fb.js"><link rel="prefetch" href="/assets/js/54.a4240c13.js"><link rel="prefetch" href="/assets/js/55.01c4b876.js"><link rel="prefetch" href="/assets/js/56.f29549a3.js"><link rel="prefetch" href="/assets/js/57.01438a04.js"><link rel="prefetch" href="/assets/js/58.29bb4626.js"><link rel="prefetch" href="/assets/js/59.e8b89408.js"><link rel="prefetch" href="/assets/js/6.8c478b39.js"><link rel="prefetch" href="/assets/js/60.bedb5381.js"><link rel="prefetch" href="/assets/js/61.bdfbe584.js"><link rel="prefetch" href="/assets/js/62.cb825d1a.js"><link rel="prefetch" href="/assets/js/63.a9516572.js"><link rel="prefetch" href="/assets/js/64.aa854ed7.js"><link rel="prefetch" href="/assets/js/65.285f883d.js"><link rel="prefetch" href="/assets/js/66.12849f37.js"><link rel="prefetch" href="/assets/js/67.c18b00e4.js"><link rel="prefetch" href="/assets/js/68.fa59daa9.js"><link rel="prefetch" href="/assets/js/69.e9d5dda4.js"><link rel="prefetch" href="/assets/js/7.cfb0d0ea.js"><link rel="prefetch" href="/assets/js/70.db126cce.js"><link rel="prefetch" href="/assets/js/71.6acb04cd.js"><link rel="prefetch" href="/assets/js/72.7ff0ab11.js"><link rel="prefetch" href="/assets/js/73.9a66da79.js"><link rel="prefetch" href="/assets/js/74.0a38a030.js"><link rel="prefetch" href="/assets/js/75.7b32dbb3.js"><link rel="prefetch" href="/assets/js/8.cb9c08e5.js"><link rel="prefetch" href="/assets/js/9.ad652cfe.js"><link rel="prefetch" href="/assets/js/vendors~flowchart.110a1065.js">
    <link rel="stylesheet" href="/assets/css/0.styles.8932179b.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/images/icons/logo.png" alt="lilei" class="logo"> <span class="site-name can-hide">lilei</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/Form/" class="nav-link">
  Form
</a></div><div class="nav-item"><a href="/IDE/" class="nav-link">
  IDE
</a></div><div class="nav-item"><a href="/Java/" class="nav-link">
  Java
</a></div><div class="nav-item"><a href="/Other/" class="nav-link">
  Other
</a></div><div class="nav-item"><a href="/Python/" class="nav-link router-link-active">
  Python
</a></div><div class="nav-item"><a href="/SQL/" class="nav-link">
  SQL
</a></div><div class="nav-item"><a href="/Shell/" class="nav-link">
  Shell
</a></div><div class="nav-item"><a href="/System/" class="nav-link">
  System
</a></div> <a href="https://github.com/woytu/notes-vuepress" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/Form/" class="nav-link">
  Form
</a></div><div class="nav-item"><a href="/IDE/" class="nav-link">
  IDE
</a></div><div class="nav-item"><a href="/Java/" class="nav-link">
  Java
</a></div><div class="nav-item"><a href="/Other/" class="nav-link">
  Other
</a></div><div class="nav-item"><a href="/Python/" class="nav-link router-link-active">
  Python
</a></div><div class="nav-item"><a href="/SQL/" class="nav-link">
  SQL
</a></div><div class="nav-item"><a href="/Shell/" class="nav-link">
  Shell
</a></div><div class="nav-item"><a href="/System/" class="nav-link">
  System
</a></div> <a href="https://github.com/woytu/notes-vuepress" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Python</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/Python/" aria-current="page" class="sidebar-link">Python</a></li><li><a href="/Python/PythonGUI.html" class="sidebar-link">PythonGUI</a></li><li><a href="/Python/Python爬虫.html" class="sidebar-link">Python爬虫</a></li><li><a href="/Python/Python笔记.html" class="active sidebar-link">Python笔记</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/Python/Python笔记.html#flag" class="sidebar-link">Flag</a></li><li class="sidebar-sub-header"><a href="/Python/Python笔记.html#字符串" class="sidebar-link">字符串</a></li><li class="sidebar-sub-header"><a href="/Python/Python笔记.html#os模块" class="sidebar-link">os模块</a></li><li class="sidebar-sub-header"><a href="/Python/Python笔记.html#shutil模块" class="sidebar-link">shutil模块</a></li><li class="sidebar-sub-header"><a href="/Python/Python笔记.html#open方法" class="sidebar-link">open方法</a></li><li class="sidebar-sub-header"><a href="/Python/Python笔记.html#进程线程协程" class="sidebar-link">进程线程协程</a></li><li class="sidebar-sub-header"><a href="/Python/Python笔记.html#orm" class="sidebar-link">ORM</a></li><li class="sidebar-sub-header"><a href="/Python/Python笔记.html#定时任务" class="sidebar-link">定时任务</a></li><li class="sidebar-sub-header"><a href="/Python/Python笔记.html#gc" class="sidebar-link">GC</a></li><li class="sidebar-sub-header"><a href="/Python/Python笔记.html#http" class="sidebar-link">HTTP</a></li><li class="sidebar-sub-header"><a href="/Python/Python笔记.html#daemon" class="sidebar-link">Daemon</a></li></ul></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="python笔记"><a href="#python笔记" class="header-anchor">#</a> Python笔记</h1> <p></p><div class="table-of-contents"><ul><li><a href="#flag">Flag</a></li><li><a href="#字符串">字符串</a></li><li><a href="#os模块">os模块</a></li><li><a href="#shutil模块">shutil模块</a></li><li><a href="#open方法">open方法</a></li><li><a href="#进程线程协程">进程线程协程</a><ul><li><a href="#进程">进程</a></li><li><a href="#线程">线程</a></li><li><a href="#线程池">线程池</a></li><li><a href="#asyncio协程">Asyncio协程</a></li></ul></li><li><a href="#orm">ORM</a></li><li><a href="#定时任务">定时任务</a></li><li><a href="#gc">GC</a></li><li><a href="#http">HTTP</a></li><li><a href="#daemon">Daemon</a><ul><li><a href="#supervisor">supervisor</a></li></ul></li></ul></div><p></p> <h2 id="flag"><a href="#flag" class="header-anchor">#</a> Flag</h2> <ul><li><a href="https://blog.csdn.net/s_daqing/article/details/104752116" target="_blank" rel="noopener noreferrer">Python程序退出方式<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.jianshu.com/p/430c411160f8" target="_blank" rel="noopener noreferrer">python：subprocess模块<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://gaianote.github.io/2018/11/01/python/1.python%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95/python%E5%AD%90%E7%A8%8B%E5%BA%8Fsubprocess" target="_blank" rel="noopener noreferrer">python子程序subprocess<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://blog.csdn.net/pcn01/article/details/104993742" target="_blank" rel="noopener noreferrer">python之pexpect模块<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://blog.csdn.net/qq_36119192/article/details/107586877" target="_blank" rel="noopener noreferrer">python中ConfigParse模块的用法<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.cnblogs.com/sui776265233/p/13299490.html" target="_blank" rel="noopener noreferrer">Python利用ConfigParser读取配置文件<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ul><li><a href="https://blog.csdn.net/h18208975507/article/details/108275293" target="_blank" rel="noopener noreferrer">python队列的使用及总结<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ol><li>列表和元组相比，可直接调用的函数更多也可以进行更改，但是元组一经定义就无法更改，所以首推列表。</li> <li>在python中是没有数组类型的，如果非要使用数组，可以用<code>numpy</code>库实现对数组的定义</li></ol> <h2 id="字符串"><a href="#字符串" class="header-anchor">#</a> 字符串</h2> <ul><li><code>r&quot;&quot;</code> r-string表示去除转义字符，即<code>\n</code>表示一个反斜杠字符，一个字母n，而不是换行。以r开头的字符，常用于正则表达式，对应re模块。</li> <li><code>f&quot;&quot;</code> f-string表示在字符串内支持大括号内的python 表达式，如果需要使用大括号字符串，则应输入连续两个大括号 <code>{{}}</code>转义</li> <li><code>b&quot;&quot;</code> b-string表示这是一个 bytes 对象</li> <li><code>u&quot;&quot;</code> u-string表示后面字符串以 Unicode 格式 进行编码，一般用在中文字符串前面，防止因为源码储存格式问题，导致再次使用时出现乱码。</li> <li><code>&quot;&quot;&quot;内容&quot;&quot;&quot;</code> 表示Multiline String 多行字符串、Template String 模板字符串、Text Blocks 文本块</li></ul> <h2 id="os模块"><a href="#os模块" class="header-anchor">#</a> os模块</h2> <ul><li><code>os.chdir(path)</code> 改变当前工作目录</li> <li><code>os.chflags(path, flags)</code> 设置路径的标记为数字标记。</li> <li><code>os.chmod(path, mode)</code> 更改权限</li> <li><code>os.chown(path, uid, gid)</code> 更改文件所有者</li> <li><code>os.chroot(path)</code> 改变当前进程的根目录</li> <li><code>os.close(fd)</code> 关闭文件描述符 fd</li> <li><code>os.closerange(fd_low, fd_high)</code> 关闭所有文件描述符，从 fd_low (包含) 到 fd_high (不包含), 错误会忽略</li> <li><code>os.dup(fd)</code> 复制文件描述符 fd</li> <li><code>os.dup2(fd, fd2)</code> 将一个文件描述符 fd 复制到另一个 fd2</li> <li><code>os.fchdir(fd)</code> 通过文件描述符改变当前工作目录</li> <li><code>os.fchmod(fd, mode)</code> 改变一个文件的访问权限，该文件由参数fd指定，参数mode是Unix下的文件访问权限。</li> <li><code>os.fchown(fd, uid, gid)</code> 修改一个文件的所有权，这个函数修改一个文件的用户ID和用户组ID，该文件由文件描述符fd指定。</li> <li><code>os.fdatasync(fd)</code> 强制将文件写入磁盘，该文件由文件描述符fd指定，但是不强制更新文件的状态信息。</li> <li><code>os.fdopen(fd[, mode[, bufsize]])</code> 通过文件描述符 fd 创建一个文件对象，并返回这个文件对象</li> <li><code>os.fpathconf(fd, name)</code> 返回一个打开的文件的系统配置信息。
<ul><li>name为检索的系统配置的值，它也许是一个定义系统值的字符串，这些名字在很多标准中指定（POSIX.1, Unix 95, Unix 98, 和其它）。</li></ul></li> <li><code>os.fstat(fd)</code> 返回文件描述符fd的状态，像stat()。</li> <li><code>os.fstatvfs(fd)</code> 返回包含文件描述符fd的文件的文件系统的信息，像 <code>statvfs()</code></li> <li><code>os.fsync(fd)</code> 强制将文件描述符为fd的文件写入硬盘。</li> <li><code>os.ftruncate(fd, length)</code> 裁剪文件描述符fd对应的文件, 所以它最大不能超过文件大小。</li> <li><code>os.getcwd()</code> 返回当前工作目录</li> <li><code>os.getcwdu()</code> 返回一个当前工作目录的Unicode对象</li> <li><code>os.isatty(fd)</code> 如果文件描述符fd是打开的，同时与<code>tty(-like)</code>设备相连，则返回<code>true</code>, 否则<code>False</code>。</li> <li><code>os.lchflags(path, flags)</code> 设置路径的标记为数字标记，类似 <code>chflags()</code>，但是没有软链接</li> <li><code>os.lchmod(path, mode)</code> 修改连接文件权限</li> <li><code>os.lchown(path, uid, gid)</code> 更改文件所有者，类似 chown，但是不追踪链接。</li> <li><code>os.link(src, dst)</code> 创建硬链接，名为参数 dst，指向参数 src</li> <li><code>os.listdir(path)</code> 返回path指定的文件夹包含的文件或文件夹的名字的列表。</li> <li><code>os.lseek(fd, pos, how)</code> 设置文件描述符 fd当前位置为pos, how方式修改: SEEK_SET 或者 0 设置从文件开始的计算的pos;
<ul><li>SEEK_CUR或者 1 则从当前位置计算; os.SEEK_END或者2则从文件尾部开始. 在unix，Windows中有效</li></ul></li> <li><code>os.lstat(path)</code> 像stat(),但是没有软链接</li> <li><code>os.major(device)</code> 从原始的设备号中提取设备major号码 (使用stat中的st_dev或者st_rdev field)。</li> <li><code>os.makedev(major, minor)</code> 以major和minor设备号组成一个原始设备号</li> <li><code>os.makedirs(path[, mode])</code> 递归文件夹创建函数。像mkdir(), 但创建的所有intermediate-level文件夹需要包含子文件夹。</li> <li><code>os.minor(device)</code> 从原始的设备号中提取设备minor号码 (使用stat中的st_dev或者st_rdev field )。</li> <li><code>os.mkdir(path[, mode])</code> 以数字mode的mode创建一个名为path的文件夹.默认的 mode 是 0777 (八进制)。</li> <li><code>os.mkfifo(path[, mode])</code> 创建命名管道，mode 为数字，默认为 0666 (八进制)`</li> <li><code>os.mknod(filename[, mode=0600, device])</code> 创建一个名为filename文件系统节点（文件，设备特别文件或者命名pipe）。</li> <li><code>os.open(file, flags[, mode])</code> 打开一个文件，并且设置需要的打开选项，mode参数是可选的</li> <li><code>os.openpty()</code> 打开一个新的伪终端对。返回 pty 和 tty的文件描述符。</li> <li><code>os.pathconf(path, name)</code> 返回相关文件的系统配置信息。</li> <li><code>os.pipe()</code> 创建一个管道. 返回一对文件描述符(r, w) 分别为读和写</li> <li><code>os.popen(command[, mode[, bufsize]])</code> 从一个 command 打开一个管道</li> <li><code>os.read(fd, n)</code> 从文件描述符 fd 中读取最多 n 个字节，返回包含读取字节的字符串，文件描述符 fd对应文件已达到结尾, 返回一个空字符串。</li> <li><code>os.readlink(path)</code> 返回软链接所指向的文件</li> <li><code>os.remove(path)</code> 删除路径为path的文件。如果path 是一个文件夹，将抛出OSError; 查看下面的rmdir()删除一个 directory。</li> <li><code>os.removedirs(path)</code> 递归删除目录。</li> <li><code>os.rename(src, dst)</code> 重命名文件或目录，从 src 到 dst</li> <li><code>os.renames(old, new)</code> 递归地对目录进行更名，也可以对文件进行更名。</li> <li><code>os.rmdir(path)</code> 删除path指定的空目录，如果目录非空，则抛出一个OSError异常。</li> <li><code>os.stat(path)</code> 获取path指定的路径的信息，功能等同于C API中的stat()系统调用。</li> <li><code>os.stat_float_times([newvalue])</code> 决定stat_result是否以float对象显示时间戳</li> <li><code>os.statvfs(path)</code> 获取指定路径的文件系统统计信息</li> <li><code>os.symlink(src, dst)</code> 创建一个软链接</li> <li><code>os.tcgetpgrp(fd)</code> 返回与终端fd（一个由os.open()返回的打开的文件描述符）关联的进程组</li> <li><code>os.tcsetpgrp(fd, pg)</code> 设置与终端fd（一个由os.open()返回的打开的文件描述符）关联的进程组为pg。</li> <li><code>os.tempnam([dir[, prefix]])</code> Python3 中已删除。返回唯一的路径名用于创建临时文件。</li> <li><code>os.tmpfile()</code> Python3 中已删除。返回一个打开的模式为(w+b)的文件对象 .这文件对象没有文件夹入口，没有文件描述符，将会自动删除。</li> <li><code>os.tmpnam()</code> Python3 中已删除。为创建一个临时文件返回一个唯一的路径</li> <li><code>os.ttyname(fd)</code> 返回一个字符串，它表示与文件描述符fd 关联的终端设备。如果fd 没有与终端设备关联，则引发一个异常。</li> <li><code>os.unlink(path)</code> 删除文件路径</li> <li><code>os.utime(path, times)</code> 返回指定的path文件的访问和修改的时间。</li> <li><code>os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])</code> 输出在文件夹中的文件名通过在树中游走，向上或者向下。</li> <li><code>os.write(fd, str)</code> 写入字符串到文件描述符 fd中. 返回实际写入的字符串长度</li> <li><code>os.path</code> 模块 获取文件的属性信息。</li></ul> <h2 id="shutil模块"><a href="#shutil模块" class="header-anchor">#</a> shutil模块</h2> <ul><li><a href="https://segmentfault.com/a/1190000016689023?utm_source=tag-newest" target="_blank" rel="noopener noreferrer">python模块之shutil<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <div class="language-python extra-class"><pre class="language-python"><code><span class="token comment"># 从源src复制到dst中去。 如果当前的dst已存在的话就会被覆盖掉</span>
shutil<span class="token punctuation">.</span>copyfile<span class="token punctuation">(</span> src<span class="token punctuation">,</span> dst<span class="token punctuation">)</span>
<span class="token comment"># 移动文件或重命名</span>
shutil<span class="token punctuation">.</span>move<span class="token punctuation">(</span> src<span class="token punctuation">,</span> dst<span class="token punctuation">)</span>
<span class="token comment"># 只是会复制其权限其他的东西是不会被复制的</span>
shutil<span class="token punctuation">.</span>copymode<span class="token punctuation">(</span> src<span class="token punctuation">,</span> dst<span class="token punctuation">)</span>
<span class="token comment"># 复制权限、最后访问时间、最后修改时间</span>
shutil<span class="token punctuation">.</span>copystat<span class="token punctuation">(</span> src<span class="token punctuation">,</span> dst<span class="token punctuation">)</span>
<span class="token comment"># 复制一个文件到一个文件或一个目录</span>
shutil<span class="token punctuation">.</span>copy<span class="token punctuation">(</span> src<span class="token punctuation">,</span> dst<span class="token punctuation">)</span>
<span class="token comment"># 在copy上的基础上再复制文件最后访问时间与修改时间也复制过来了，类似于cp –p的东西</span>
shutil<span class="token punctuation">.</span>copy2<span class="token punctuation">(</span> src<span class="token punctuation">,</span> dst<span class="token punctuation">)</span>
<span class="token comment"># 如果两个位置的文件系统是一样的话相当于是rename操作，只是改名；如果是不在相同的文件系统的话就是做move操作</span>
shutil<span class="token punctuation">.</span>copy2<span class="token punctuation">(</span> src<span class="token punctuation">,</span> dst<span class="token punctuation">)</span>
<span class="token comment"># 把olddir拷贝一份newdir，如果第3个参数是True，则复制目录时将保持文件夹下的符号连接，</span>
<span class="token comment"># 如果第3个参数是False，则将在复制的目录下生成物理副本来替代符号连接</span>
shutil<span class="token punctuation">.</span>copytree<span class="token punctuation">(</span> olddir<span class="token punctuation">,</span> newdir<span class="token punctuation">,</span> <span class="token boolean">True</span><span class="token operator">/</span>Flase<span class="token punctuation">)</span>
<span class="token comment"># 递归删除一个目录以及目录内的所有内容</span>
shutil<span class="token punctuation">.</span>rmtree<span class="token punctuation">(</span> src <span class="token punctuation">)</span>
</code></pre></div><h2 id="open方法"><a href="#open方法" class="header-anchor">#</a> open方法</h2> <table><thead><tr><th>模式</th> <th>描述</th></tr></thead> <tbody><tr><td>r</td> <td>以只读方式打开文件。文件的指针将会放在文件的开头。这是默认模式。</td></tr> <tr><td>rb</td> <td>以二进制格式打开一个文件用于只读。文件指针将会放在文件的开头。</td></tr> <tr><td>r+</td> <td>打开一个文件用于读写。文件指针将会放在文件的开头。</td></tr> <tr><td>rb+</td> <td>以二进制格式打开一个文件用于读写。文件指针将会放在文件的开头。</td></tr> <tr><td>w</td> <td>打开一个文件只用于写入。如果该文件已存在则打开文件，并从开头开始编辑，即原有内容会被删除。如果该文件不存在，创建新文件。</td></tr> <tr><td>wb</td> <td>以二进制格式打开一个文件只用于写入。如果该文件已存在则打开文件，并从开头开始编辑，即原有内容会被删除。如果该文件不存在，创建新文件。</td></tr> <tr><td>w+</td> <td>打开一个文件用于读写。如果该文件已存在则打开文件，并从开头开始编辑，即原有内容会被删除。如果该文件不存在，创建新文件。</td></tr> <tr><td>wb+</td> <td>以二进制格式打开一个文件用于读写。如果该文件已存在则打开文件，并从开头开始编辑，即原有内容会被删除。如果该文件不存在，创建新文件。</td></tr> <tr><td>a</td> <td>打开一个文件用于追加。如果该文件已存在，文件指针将会放在文件的结尾。也就是说，新的内容将会被写入到已有内容之后。如果该文件不存在，创建新文件进行写入。</td></tr> <tr><td>ab</td> <td>以二进制格式打开一个文件用于追加。如果该文件已存在，文件指针将会放在文件的结尾。也就是说，新的内容将会被写入到已有内容之后。如果该文件不存在，创建新文件进行写入。</td></tr> <tr><td>a+</td> <td>打开一个文件用于读写。如果该文件已存在，文件指针将会放在文件的结尾。文件打开时会是追加模式。如果该文件不存在，创建新文件用于读写。</td></tr> <tr><td>ab+</td> <td>以二进制格式打开一个文件用于追加。如果该文件已存在，文件指针将会放在文件的结尾。如果该文件不存在，创建新文件用于读写。</td></tr></tbody></table> <ul><li><a href="https://www.jianshu.com/p/507b14111f56?from=timeline" target="_blank" rel="noopener noreferrer">Python3 文件 I/O 操作<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <h2 id="进程线程协程"><a href="#进程线程协程" class="header-anchor">#</a> 进程线程协程</h2> <h3 id="进程"><a href="#进程" class="header-anchor">#</a> 进程</h3> <ul><li><a href="https://docs.python.org/zh-cn/3/library/multiprocessing.html" target="_blank" rel="noopener noreferrer">multiprocessing --- 基于进程的并行<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://morvanzhou.github.io/tutorials/python-basic/multiprocessing" target="_blank" rel="noopener noreferrer">multiprocessing<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> multiprocessing <span class="token keyword">as</span> mp
threads <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> path<span class="token punctuation">:</span>
  threads<span class="token punctuation">.</span>append<span class="token punctuation">(</span>mp<span class="token punctuation">.</span>Process<span class="token punctuation">(</span>target<span class="token operator">=</span>md5sum<span class="token punctuation">,</span>args<span class="token operator">=</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment">#创建一个multiprocessing.process.Process对象</span>
<span class="token comment">#执行</span>
<span class="token keyword">for</span> m <span class="token keyword">in</span> threads<span class="token punctuation">:</span>
  m<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">#回收</span>
<span class="token keyword">for</span> m <span class="token keyword">in</span> threads<span class="token punctuation">:</span>
  m<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre></div><div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">def</span>  <span class="token function">test</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">:</span>
  <span class="token keyword">pass</span>
<span class="token comment"># 创建一个multiprocessing.pool.Pool的对象</span>
p1 <span class="token operator">=</span> mp<span class="token punctuation">.</span>Pool<span class="token punctuation">(</span>processes <span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">)</span>
<span class="token comment">#向进程池里添加任务</span>
<span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
  p1<span class="token punctuation">.</span>apply_async<span class="token punctuation">(</span>test<span class="token punctuation">,</span> args<span class="token operator">=</span><span class="token punctuation">(</span>i<span class="token punctuation">,</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token comment">#func，参数（传入元组）</span>
<span class="token comment"># 关闭进程池，不再接受请求</span>
p1<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># 等待所有子进程结束</span>
p1<span class="token punctuation">.</span>join<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre></div><h3 id="线程"><a href="#线程" class="header-anchor">#</a> 线程</h3> <ul><li><a href="https://morvanzhou.github.io/tutorials/python-basic/threading" target="_blank" rel="noopener noreferrer">多线程<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> threading
<span class="token comment"># 使用新线程执行函数</span>
threading<span class="token punctuation">.</span>Thread<span class="token punctuation">(</span>target<span class="token operator">=</span>test<span class="token punctuation">,</span> args<span class="token operator">=</span><span class="token punctuation">(</span><span class="token string">&quot;test&quot;</span><span class="token punctuation">,</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># 在指定的秒数后调用一个函数</span>
threading<span class="token punctuation">.</span>Timer<span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> test<span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token string">&quot;test&quot;</span><span class="token punctuation">,</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre></div><h3 id="线程池"><a href="#线程池" class="header-anchor">#</a> 线程池</h3> <ul><li><a href="https://blog.51cto.com/445153/2138832" target="_blank" rel="noopener noreferrer">python3异步编程<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.jianshu.com/p/13d2e8514546" target="_blank" rel="noopener noreferrer">Python 并发、并行、同步和异步<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.jianshu.com/p/6d6e4f745c27" target="_blank" rel="noopener noreferrer">ThreadPoolExecutor<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">from</span> concurrent<span class="token punctuation">.</span>futures <span class="token keyword">import</span> ThreadPoolExecutor
<span class="token keyword">import</span> multiprocessing
pool <span class="token operator">=</span> ThreadPoolExecutor<span class="token punctuation">(</span>max_workers<span class="token operator">=</span><span class="token builtin">int</span><span class="token punctuation">(</span>multiprocessing<span class="token punctuation">.</span>cpu_count<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">-</span> <span class="token number">0.9</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
done <span class="token operator">=</span> pool<span class="token punctuation">.</span>submit<span class="token punctuation">(</span>test<span class="token punctuation">,</span> <span class="token string">&quot;test&quot;</span><span class="token punctuation">)</span>
<span class="token comment"># 执行完成调用函数</span>
done<span class="token punctuation">.</span>add_done_callback<span class="token punctuation">(</span>thread_call_back<span class="token punctuation">)</span>
</code></pre></div><h3 id="asyncio协程"><a href="#asyncio协程" class="header-anchor">#</a> Asyncio协程</h3> <ul><li><a href="https://www.liaoxuefeng.com/wiki/1016959663602400/1017959540289152" target="_blank" rel="noopener noreferrer">异步IO<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://thief.one/2018/06/21/1" target="_blank" rel="noopener noreferrer">Python3.5协程学习研究<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.ibm.com/developerworks/cn/analytics/library/ba-on-demand-data-python-3/index.html?ca=drs-" target="_blank" rel="noopener noreferrer">协程和 asyncio<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.jianshu.com/p/9ce6c3bf6733" target="_blank" rel="noopener noreferrer">python asyncio<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://zhuanlan.zhihu.com/p/59621713" target="_blank" rel="noopener noreferrer">Python中协程异步IO（asyncio）详解<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://docs.python.org/zh-cn/3/library/asyncio.html" target="_blank" rel="noopener noreferrer">https://docs.python.org/zh-cn/3/library/asyncio.html<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="http://www.langzi.fun/Asyncio%E5%B9%B6%E5%8F%91%E7%BC%96%E7%A8%8B.html" target="_blank" rel="noopener noreferrer">Asyncio并发编程<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://blog.csdn.net/dashoumeixi/article/details/88800916" target="_blank" rel="noopener noreferrer">aiohttp爬虫<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://blog.csdn.net/dashoumeixi/article/details/81085141" target="_blank" rel="noopener noreferrer">python asyncio aiohttp 异步下载 完整例子<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://blog.csdn.net/dashoumeixi/article/details/80938327" target="_blank" rel="noopener noreferrer">python 多线程与asyncio下载文件<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="http://www.laitech.cn/2018/08/08/393" target="_blank" rel="noopener noreferrer">python写一个多线程下载程序<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://blog.csdn.net/dashoumeixi/article/details/88845137" target="_blank" rel="noopener noreferrer">关于aiohttp下载大文件的方式<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://madmalls.com/blog/post/aiohttp-howto-in-python3" target="_blank" rel="noopener noreferrer">使用 asyncio + aiohttp 并发下载<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> threading
<span class="token keyword">import</span> asyncio
 
<span class="token keyword">async</span> <span class="token keyword">def</span> <span class="token function">hello</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Hello world! (%s)'</span> <span class="token operator">%</span> threading<span class="token punctuation">.</span>currentThread<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token keyword">await</span> asyncio<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Hello again! (%s)'</span> <span class="token operator">%</span> threading<span class="token punctuation">.</span>currentThread<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
 
 
<span class="token keyword">try</span><span class="token punctuation">:</span>
    loop <span class="token operator">=</span> asyncio<span class="token punctuation">.</span>get_event_loop<span class="token punctuation">(</span><span class="token punctuation">)</span>
    tasks <span class="token operator">=</span> <span class="token punctuation">[</span>hello<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">for</span> i <span class="token keyword">in</span> <span class="token builtin">range</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token operator">*</span><span class="token number">1000</span><span class="token operator">*</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">]</span>
    loop<span class="token punctuation">.</span>run_until_complete<span class="token punctuation">(</span>asyncio<span class="token punctuation">.</span>wait<span class="token punctuation">(</span>tasks<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token comment"># loop.close()</span>
    <span class="token comment"># Python 3.7</span>
    <span class="token comment">#asyncio.run(asyncio.wait(tasks))</span>
<span class="token keyword">except</span> ValueError<span class="token punctuation">:</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'Async Error'</span><span class="token punctuation">)</span>
</code></pre></div><h2 id="orm"><a href="#orm" class="header-anchor">#</a> ORM</h2> <blockquote><p><code>Object-Relational Mapping</code>，把关系数据库的表结构映射到对象上。</p></blockquote> <ul><li>Peewee</li> <li>SqlObject</li></ul> <p><strong>SQLite3</strong></p> <ul><li><a href="https://docs.python.org/zh-cn/3.8/library/sqlite3.html" target="_blank" rel="noopener noreferrer">https://docs.python.org/zh-cn/3.8/library/sqlite3.html<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.runoob.com/sqlite/sqlite-python.html" target="_blank" rel="noopener noreferrer">SQLite - Python<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.yiibai.com/sqlite/python-with-sqlite.html" target="_blank" rel="noopener noreferrer">Python连接SQLite数据库<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <p><strong>SQLAlchemy</strong></p> <ul><li><a href="https://www.sqlalchemy.org" target="_blank" rel="noopener noreferrer">https://www.sqlalchemy.org<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.liaoxuefeng.com/wiki/1016959663602400/1017803857459008" target="_blank" rel="noopener noreferrer">使用SQLAlchemy<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <div class="language-python extra-class"><pre class="language-python"><code>pip install sqlalchemy
</code></pre></div><h2 id="定时任务"><a href="#定时任务" class="header-anchor">#</a> 定时任务</h2> <ul><li><a href="https://blog.51cto.com/huangyg/2367088" target="_blank" rel="noopener noreferrer">Python3-定时任务五种实现方式<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <ol><li>sched模块<code>sched.scheduler</code></li> <li>循环+<code>time.sleep()</code>、<code>asyncio.sleep()</code> 用的是<code>application scheduler</code></li> <li>线程模块中Timer类<code>threading.Timer</code> 用的是<code>system scheduler</code></li> <li>第三方模块：schedule</li> <li>定时框架：APScheduler、Celery</li></ol> <h2 id="gc"><a href="#gc" class="header-anchor">#</a> GC</h2> <ul><li><a href="https://docs.python.org/zh-cn/3.8/library/gc.html" target="_blank" rel="noopener noreferrer">https://docs.python.org/zh-cn/3.8/library/gc.html<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://docs.python.org/zh-cn/3/library/tracemalloc.html" target="_blank" rel="noopener noreferrer">https://docs.python.org/zh-cn/3/library/tracemalloc.html<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/giampaolo/psutil" target="_blank" rel="noopener noreferrer">https://github.com/giampaolo/psutil<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/pythonprofilers/memory_profiler" target="_blank" rel="noopener noreferrer">https://github.com/pythonprofilers/memory_profiler<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/mgedmin/objgraph" target="_blank" rel="noopener noreferrer">https://github.com/mgedmin/objgraph<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/pympler/pympler" target="_blank" rel="noopener noreferrer">https://github.com/pympler/pympler<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/zhuyifei1999/guppy3" target="_blank" rel="noopener noreferrer">https://github.com/zhuyifei1999/guppy3<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://github.com/benfred/py-spy" target="_blank" rel="noopener noreferrer">https://github.com/benfred/py-spy<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://saucer-man.com/operation_and_maintenance/298.html" target="_blank" rel="noopener noreferrer">一次python内存调优经历<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <blockquote><p>使用<code>sys.getsizeof</code>方法可以查看<code>python</code>对象的内存占用，单位：字节 (byte)</p></blockquote> <div class="language-python extra-class"><pre class="language-python"><code><span class="token comment">#开启gc（默认）</span>
gc<span class="token punctuation">.</span>enable<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># 关闭gc</span>
gc<span class="token punctuation">.</span>disable<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># 判断gc是否开启</span>
gc<span class="token punctuation">.</span>isenabled<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># 执行一次垃圾回收，不管gc是否处于开启状态都能使用</span>
gc<span class="token punctuation">.</span>collect<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># 设置垃圾回收阈值</span>
gc<span class="token punctuation">.</span>set_threshold<span class="token punctuation">(</span>t0<span class="token punctuation">,</span> t1<span class="token punctuation">,</span> t2<span class="token punctuation">)</span> 
<span class="token comment"># 获得当前的垃圾回收阈值</span>
gc<span class="token punctuation">.</span>get_threshold<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># 获取所有被垃圾回收器监控管理的对象</span>
gc<span class="token punctuation">.</span>get_objects<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># 返回obj对象直接指向的对象</span>
gc<span class="token punctuation">.</span>get_referents<span class="token punctuation">(</span>obj<span class="token punctuation">)</span>
<span class="token comment"># 返回所有直接指向obj的对象</span>
gc<span class="token punctuation">.</span>get_referrers<span class="token punctuation">(</span>obj<span class="token punctuation">)</span>
</code></pre></div><div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> tracemalloc

tracemalloc<span class="token punctuation">.</span>start<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 开始跟踪内存分配</span>
time1 <span class="token operator">=</span> tracemalloc<span class="token punctuation">.</span>take_snapshow<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">import</span> waste_memory
x <span class="token operator">=</span> waste_memory<span class="token punctuation">.</span>run<span class="token punctuation">(</span><span class="token punctuation">)</span>
time2 <span class="token operator">=</span> tracemalloc<span class="token punctuation">.</span>take_snapshow<span class="token punctuation">(</span><span class="token punctuation">)</span>
stats <span class="token operator">=</span> time2<span class="token punctuation">.</span>compare_to<span class="token punctuation">(</span>time1<span class="token punctuation">,</span> <span class="token string">'lineno'</span><span class="token punctuation">)</span>
snapshot <span class="token operator">=</span> tracemalloc<span class="token punctuation">.</span>take_snapshot<span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment"># 快照，当前内存分配</span>
top_stats <span class="token operator">=</span> snapshot<span class="token punctuation">.</span>statistics<span class="token punctuation">(</span><span class="token string">'lineno'</span><span class="token punctuation">)</span>  <span class="token comment"># 快照对象的统计</span>
<span class="token punctuation">[</span><span class="token keyword">print</span><span class="token punctuation">(</span>stat<span class="token punctuation">)</span> <span class="token keyword">for</span> stat <span class="token keyword">in</span> top_stats<span class="token punctuation">]</span>
</code></pre></div><h2 id="http"><a href="#http" class="header-anchor">#</a> HTTP</h2> <ul><li><a href="https://docs.python.org/3/library/urllib.html" target="_blank" rel="noopener noreferrer">urllib — URL handling modules<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://docs.python.org/3/library/http.html" target="_blank" rel="noopener noreferrer">http — HTTP modules<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://www.cnblogs.com/superhin/p/11455240.html" target="_blank" rel="noopener noreferrer">Python3 内置http.client,urllib.request及三方库requests发送请求对比<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li> <li><a href="https://blog.csdn.net/jiduochou963/article/details/87564467" target="_blank" rel="noopener noreferrer">Python urllib、urllib2、urllib3用法及区别<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> http<span class="token punctuation">.</span>client

<span class="token comment"># 建立HTTP连接</span>
conn <span class="token operator">=</span> http<span class="token punctuation">.</span>client<span class="token punctuation">.</span>HTTPConnection<span class="token punctuation">(</span><span class="token string">&quot;api.github.com&quot;</span><span class="token punctuation">,</span> timeout<span class="token operator">=</span><span class="token number">30</span><span class="token punctuation">)</span>

USER_AGENT <span class="token operator">=</span> <span class="token string">&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36&quot;</span>
<span class="token comment"># 发送GET请求，制定接口路径</span>
conn<span class="token punctuation">.</span>request<span class="token punctuation">(</span>method<span class="token operator">=</span><span class="token string">&quot;GET&quot;</span><span class="token punctuation">,</span> url<span class="token operator">=</span><span class="token string">'/repos/rclone/rclone/releases/latest'</span><span class="token punctuation">,</span> headers<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">&quot;User-Agent&quot;</span><span class="token punctuation">:</span> USER_AGENT<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment"># 获取响应</span>
res <span class="token operator">=</span> conn<span class="token punctuation">.</span>getresponse<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment">#  解析相应.进行解码</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>decode<span class="token punctuation">(</span><span class="token string">&quot;utf-8&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></div><div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> ssl
<span class="token comment"># 如果import urllib，在使用urllib.request时会报错</span>
<span class="token keyword">import</span> urllib<span class="token punctuation">.</span>request


<span class="token comment"># 不验证证书，或者打开Python3文件夹, 执行 Install Certificates.command 文件</span>
ssl<span class="token punctuation">.</span>_create_default_https_context <span class="token operator">=</span> ssl<span class="token punctuation">.</span>_create_unverified_context

USER_AGENT <span class="token operator">=</span> <span class="token string">&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36&quot;</span>
req <span class="token operator">=</span> urllib<span class="token punctuation">.</span>request<span class="token punctuation">.</span>Request<span class="token punctuation">(</span><span class="token string">&quot;https://api.github.com/repos/rclone/rclone/releases/latest&quot;</span><span class="token punctuation">,</span>
                             headers<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">&quot;User-Agent&quot;</span><span class="token punctuation">:</span> USER_AGENT<span class="token punctuation">}</span><span class="token punctuation">,</span> method<span class="token operator">=</span><span class="token string">'GET'</span><span class="token punctuation">)</span>
res <span class="token operator">=</span> urllib<span class="token punctuation">.</span>request<span class="token punctuation">.</span>urlopen<span class="token punctuation">(</span>req<span class="token punctuation">,</span> timeout<span class="token operator">=</span><span class="token number">30</span><span class="token punctuation">)</span>
<span class="token comment"># 解析相应.进行解码</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span>read<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>decode<span class="token punctuation">(</span><span class="token string">&quot;utf-8&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span>

<span class="token comment">########## 下载文件 ##########</span>
<span class="token comment"># 创建一个opener对象</span>
opener <span class="token operator">=</span> urllib<span class="token punctuation">.</span>request<span class="token punctuation">.</span>build_opener<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># 向opener传入请求头信息</span>
opener<span class="token punctuation">.</span>addheaders<span class="token punctuation">.</span>append<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token string">'User-agent'</span><span class="token punctuation">,</span> USER_AGENT<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token comment"># 将创建好的opener对象装入request</span>
urllib<span class="token punctuation">.</span>request<span class="token punctuation">.</span>install_opener<span class="token punctuation">(</span>opener<span class="token punctuation">)</span>
filename<span class="token punctuation">,</span> res <span class="token operator">=</span> urllib<span class="token punctuation">.</span>request<span class="token punctuation">.</span>urlretrieve<span class="token punctuation">(</span><span class="token string">&quot;https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;test.png&quot;</span><span class="token punctuation">)</span>
<span class="token comment"># 从urlretrieve调用中清理临时文件</span>
urllib<span class="token punctuation">.</span>request<span class="token punctuation">.</span>urlcleanup<span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token comment"># 另一种方式</span>
<span class="token comment"># opener = urllib.request.URLopener()</span>
<span class="token comment"># 带有我们可以处理的错误处理程序的派生类</span>
opener <span class="token operator">=</span> urllib<span class="token punctuation">.</span>request<span class="token punctuation">.</span>FancyURLopener<span class="token punctuation">(</span><span class="token punctuation">)</span>
opener<span class="token punctuation">.</span>version <span class="token operator">=</span> user_agent
filename <span class="token operator">=</span> opener<span class="token punctuation">.</span>retrieve<span class="token punctuation">(</span><span class="token string">&quot;https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;test.png&quot;</span><span class="token punctuation">)</span>
</code></pre></div><div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> urllib3
<span class="token keyword">import</span> json
 
data <span class="token operator">=</span> json<span class="token punctuation">.</span>dumps<span class="token punctuation">(</span><span class="token punctuation">{</span><span class="token string">'abc'</span><span class="token punctuation">:</span> <span class="token string">'123'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
http <span class="token operator">=</span> urllib3<span class="token punctuation">.</span>PoolManager<span class="token punctuation">(</span>num_pools<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">,</span> headers<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">'User-Agent'</span><span class="token punctuation">:</span> <span class="token string">'ABCDE'</span><span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token comment"># 使用代理来访问某个网站</span>
<span class="token comment"># http = urllib3.ProxyManager('http://127.0.0.1:800', headers={'User-Agent': 'ABCDE'})</span>

<span class="token comment"># request使用fields进行POST请求不需要将 data参数转换成JSON格式，GET请求不需要自己拼接url参数</span>
<span class="token comment"># 不过特别要声明的一点是 fielder 和 body 中只能存在一个</span>
resp1 <span class="token operator">=</span> http<span class="token punctuation">.</span>request<span class="token punctuation">(</span><span class="token string">'POST'</span><span class="token punctuation">,</span> <span class="token string">'http://www.httpbin.org/post'</span><span class="token punctuation">,</span> body<span class="token operator">=</span>data<span class="token punctuation">,</span> timeout<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">,</span> retries<span class="token operator">=</span><span class="token number">5</span><span class="token punctuation">,</span> fields<span class="token operator">=</span>data<span class="token punctuation">)</span>
<span class="token comment">#resp2 = http.urlopen('POST', 'http://www.httpbin.org/post', body=data, timeout=5, retries=5)</span>
 
<span class="token keyword">print</span><span class="token punctuation">(</span>resp1<span class="token punctuation">.</span>data<span class="token punctuation">.</span>decode<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></div><p><strong>提交form-data表单</strong></p> <ul><li>通过<code>files</code>参数传递<code>form-data</code></li></ul> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> requests
<span class="token keyword">from</span> requests<span class="token punctuation">.</span>packages<span class="token punctuation">.</span>urllib3<span class="token punctuation">.</span>exceptions <span class="token keyword">import</span> InsecureRequestWarning

USER_AGENT <span class="token operator">=</span> <span class="token string">&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) &quot;</span> \
             <span class="token string">&quot;Chrome/77.0.3865.75 Safari/537.36 &quot;</span>


<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    <span class="token comment"># 移除不验证SSL的警告</span>
    requests<span class="token punctuation">.</span>packages<span class="token punctuation">.</span>urllib3<span class="token punctuation">.</span>disable_warnings<span class="token punctuation">(</span>InsecureRequestWarning<span class="token punctuation">)</span>
    data <span class="token operator">=</span> <span class="token punctuation">{</span>
        <span class="token string">&quot;_wpcf7&quot;</span><span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token boolean">None</span><span class="token punctuation">,</span> <span class="token string">&quot;3016&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token string">&quot;_wpcf7_version&quot;</span><span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token boolean">None</span><span class="token punctuation">,</span> <span class="token string">&quot;5.1.1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token string">&quot;_wpcf7_locale&quot;</span><span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token boolean">None</span><span class="token punctuation">,</span> <span class="token string">&quot;en_US&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        <span class="token string">&quot;_wpcf7_unit_tag&quot;</span><span class="token punctuation">:</span> <span class="token punctuation">(</span><span class="token boolean">None</span><span class="token punctuation">,</span> <span class="token string">&quot;wpcf7-f3016-p4203-o2&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span>
    <span class="token comment"># verify=False移除SSL认证</span>
    res <span class="token operator">=</span> requests<span class="token punctuation">.</span>post<span class="token punctuation">(</span><span class="token string">&quot;https://www.netsarang.com/json/download/process.html&quot;</span><span class="token punctuation">,</span> files<span class="token operator">=</span>data<span class="token punctuation">,</span>
                        headers<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">&quot;User-Agent&quot;</span><span class="token punctuation">:</span> USER_AGENT<span class="token punctuation">}</span><span class="token punctuation">,</span> verify<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">,</span> timeout<span class="token operator">=</span><span class="token number">600</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span>text<span class="token punctuation">)</span>
</code></pre></div><ul><li>手动组建<code>form-data</code>并加上<code>headers</code></li></ul> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">import</span> requests

USER_AGENT <span class="token operator">=</span> <span class="token string">&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) &quot;</span> \
             <span class="token string">&quot;Chrome/77.0.3865.75 Safari/537.36 &quot;</span>


<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    data <span class="token operator">=</span> <span class="token punctuation">{</span>
        <span class="token string">&quot;_wpcf7&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;3016&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;_wpcf7_version&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;5.1.1&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;_wpcf7_locale&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;en_US&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;_wpcf7_unit_tag&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;wpcf7-f3016-p4203-o2&quot;</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span>
    payload <span class="token operator">=</span> <span class="token string">&quot;&quot;</span>
    boundary <span class="token operator">=</span> <span class="token string">&quot;----WebKitFormBoundary67TaDgLkWD9HrhlW&quot;</span>
    <span class="token keyword">for</span> k<span class="token punctuation">,</span> v <span class="token keyword">in</span> data<span class="token punctuation">.</span>items<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
        payload <span class="token operator">+=</span> <span class="token string-interpolation"><span class="token string">f&quot;--</span><span class="token interpolation"><span class="token punctuation">{</span>boundary<span class="token punctuation">}</span></span><span class="token string">\r\nContent-Disposition: form-data; name=\&quot;</span><span class="token interpolation"><span class="token punctuation">{</span>k<span class="token punctuation">}</span></span><span class="token string">\&quot;\r\n\r\n</span><span class="token interpolation"><span class="token punctuation">{</span>v<span class="token punctuation">}</span></span><span class="token string">\r\n&quot;</span></span>
    payload <span class="token operator">+=</span> <span class="token string-interpolation"><span class="token string">f&quot;</span><span class="token interpolation"><span class="token punctuation">{</span>boundary<span class="token punctuation">}</span></span><span class="token string">--&quot;</span></span>
    res <span class="token operator">=</span> requests<span class="token punctuation">.</span>post<span class="token punctuation">(</span><span class="token string">&quot;https://www.netsarang.com/json/download/process.html&quot;</span><span class="token punctuation">,</span> payload<span class="token punctuation">,</span>
                        headers<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">&quot;User-Agent&quot;</span><span class="token punctuation">:</span> USER_AGENT<span class="token punctuation">,</span><span class="token string">&quot;Content-Type&quot;</span><span class="token punctuation">:</span> <span class="token string-interpolation"><span class="token string">f&quot;multipart/form-data; boundary=</span><span class="token interpolation"><span class="token punctuation">{</span>boundary<span class="token punctuation">}</span></span><span class="token string">&quot;</span></span><span class="token punctuation">}</span><span class="token punctuation">,</span>
                        verify<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">,</span> timeout<span class="token operator">=</span><span class="token number">600</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span>text<span class="token punctuation">)</span>
</code></pre></div><ul><li>使用<code>encode_multipart_formdata</code></li></ul> <div class="language-python extra-class"><pre class="language-python"><code><span class="token keyword">from</span> urllib3 <span class="token keyword">import</span> encode_multipart_formdata
<span class="token keyword">import</span> requests

USER_AGENT <span class="token operator">=</span> <span class="token string">&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) &quot;</span> \
             <span class="token string">&quot;Chrome/77.0.3865.75 Safari/537.36 &quot;</span>


<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    data <span class="token operator">=</span> <span class="token punctuation">{</span>
        <span class="token string">&quot;_wpcf7&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;3016&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;_wpcf7_version&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;5.1.1&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;_wpcf7_locale&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;en_US&quot;</span><span class="token punctuation">,</span>
        <span class="token string">&quot;_wpcf7_unit_tag&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;wpcf7-f3016-p4203-o2&quot;</span><span class="token punctuation">,</span>
    <span class="token punctuation">}</span>
    <span class="token comment"># 注意这里的 - 符号的数量，默认有两个</span>
    <span class="token punctuation">(</span>payload<span class="token punctuation">,</span> content_type<span class="token punctuation">)</span> <span class="token operator">=</span> encode_multipart_formdata<span class="token punctuation">(</span>data<span class="token punctuation">,</span> boundary<span class="token operator">=</span><span class="token string">'----WebKitFormBoundary67TaDgLkWD9HrhlW'</span><span class="token punctuation">)</span>
    res <span class="token operator">=</span> requests<span class="token punctuation">.</span>post<span class="token punctuation">(</span><span class="token string">&quot;https://www.netsarang.com/json/download/process.html&quot;</span><span class="token punctuation">,</span> payload<span class="token punctuation">,</span>
                        headers<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">&quot;User-Agent&quot;</span><span class="token punctuation">:</span> HttpUtil<span class="token punctuation">.</span>USER_AGENT<span class="token punctuation">,</span> <span class="token string">&quot;Content-Type&quot;</span><span class="token punctuation">:</span> content_type<span class="token punctuation">}</span><span class="token punctuation">,</span> verify<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">,</span>
                        timeout<span class="token operator">=</span><span class="token number">600</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span>text<span class="token punctuation">)</span>
</code></pre></div><ul><li>使用<code>requests-toolbelt</code></li></ul> <div class="language-python extra-class"><pre class="language-python"><code><span class="token comment"># pip install requests-toolbelt</span>
<span class="token keyword">from</span> requests_toolbelt <span class="token keyword">import</span> MultipartEncoder
<span class="token keyword">import</span> requests

USER_AGENT <span class="token operator">=</span> <span class="token string">&quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) &quot;</span> \
             <span class="token string">&quot;Chrome/77.0.3865.75 Safari/537.36 &quot;</span>


<span class="token keyword">if</span> __name__ <span class="token operator">==</span> <span class="token string">'__main__'</span><span class="token punctuation">:</span>
    me <span class="token operator">=</span> MultipartEncoder<span class="token punctuation">(</span>
        fields<span class="token operator">=</span><span class="token punctuation">{</span>
            <span class="token string">&quot;_wpcf7&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;3016&quot;</span><span class="token punctuation">,</span>
            <span class="token string">&quot;_wpcf7_version&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;5.1.1&quot;</span><span class="token punctuation">,</span>
            <span class="token string">&quot;_wpcf7_locale&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;en_US&quot;</span><span class="token punctuation">,</span>
            <span class="token string">&quot;_wpcf7_unit_tag&quot;</span><span class="token punctuation">:</span> <span class="token string">&quot;wpcf7-f3016-p4203-o2&quot;</span><span class="token punctuation">,</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">)</span>
    res <span class="token operator">=</span> requests<span class="token punctuation">.</span>post<span class="token punctuation">(</span><span class="token string">&quot;https://www.netsarang.com/json/download/process.html&quot;</span><span class="token punctuation">,</span> me<span class="token punctuation">,</span>
                        headers<span class="token operator">=</span><span class="token punctuation">{</span><span class="token string">&quot;User-Agent&quot;</span><span class="token punctuation">:</span> USER_AGENT<span class="token punctuation">,</span> <span class="token string">&quot;Content-Type&quot;</span><span class="token punctuation">:</span> me<span class="token punctuation">.</span>content_type<span class="token punctuation">}</span><span class="token punctuation">,</span> verify<span class="token operator">=</span><span class="token boolean">False</span><span class="token punctuation">,</span>
                        timeout<span class="token operator">=</span><span class="token number">600</span><span class="token punctuation">)</span>
    <span class="token keyword">print</span><span class="token punctuation">(</span>res<span class="token punctuation">.</span>text<span class="token punctuation">)</span>
</code></pre></div><h2 id="daemon"><a href="#daemon" class="header-anchor">#</a> Daemon</h2> <ul><li><a href="/Go/Go笔记.html#daemon">Golang版Daemon更加简易可用</a></li> <li><a href="https://cloud.tencent.com/developer/article/1567443" target="_blank" rel="noopener noreferrer">Python守护进程daemon实现<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul> <h3 id="supervisor"><a href="#supervisor" class="header-anchor">#</a> supervisor</h3> <blockquote><p><code>supervisor</code>是用<code>Python</code>开发的一套通用的进程管理程序，能将一个普通的命令行进程变为后台<code>daemon</code>，并监控进程状态，异常退出时能自动重启。</p></blockquote> <ul><li><a href="https://github.com/Supervisor/supervisor" target="_blank" rel="noopener noreferrer">https://github.com/Supervisor/supervisor<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> <ul><li><a href="http://supervisord.org" target="_blank" rel="noopener noreferrer">http://supervisord.org<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></li></ul> <p><strong>常见配置如下</strong></p> <div class="language-ini extra-class"><pre class="language-ini"><code><span class="token header"><span class="token punctuation">[</span><span class="token section-name selector">program:程序名称</span><span class="token punctuation">]</span></span>
<span class="token key attr-name">user</span><span class="token punctuation">=</span><span class="token value attr-value">root</span>
<span class="token key attr-name">command</span><span class="token punctuation">=</span><span class="token value attr-value">/var/www/main</span>
<span class="token key attr-name">stdout_logfile</span><span class="token punctuation">=</span><span class="token value attr-value">/var/log/gf-app-stdout.log</span>
<span class="token key attr-name">stderr_logfile</span><span class="token punctuation">=</span><span class="token value attr-value">/var/log/gf-app-stderr.log</span>
<span class="token key attr-name">autostart</span><span class="token punctuation">=</span><span class="token value attr-value">true</span>
<span class="token key attr-name">autorestart</span><span class="token punctuation">=</span><span class="token value attr-value">true</span>
</code></pre></div><p><strong>使用步骤</strong></p> <ol><li>使用<code>sudo service supervisor start</code>启动<code>supervisor</code>服务；</li> <li>创建应用配置文件<code>/etc/supervisor/conf.d/程序名称.conf</code>, 内容如上;</li> <li>使用<code>sudo supervisorctl</code>进入<code>supervisor</code>管理终端；</li> <li>使用<code>reload</code>重新读取配置文件并重启当前<code>supoervisor</code>管理的所有进程；</li> <li>也可以使用<code>update</code>重新加载配置(默认不重启)，随后使用<code>start 程序名称</code>启动指定的应用程序；</li> <li>随后可以使用<code>status</code>指令查看当前<code>supervisor</code>管理的进程状态；</li></ol></div> <footer class="page-edit"><div class="edit-link"><a href="https://github.com/woytu/notes-vuepress/edit/master//Python/Python笔记.md" target="_blank" rel="noopener noreferrer">在 GitHub 上编辑此页</a> <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></div> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2021-07-23 21:57:59</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/Python/Python爬虫.html" class="prev">
        Python爬虫
      </a></span> <!----></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.269009d6.js" defer></script><script src="/assets/js/3.974b9720.js" defer></script><script src="/assets/js/45.79178b33.js" defer></script>
  </body>
</html>
